﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace HPortal.Module
{
    public partial class ArticleList : PanelControl
    {   
        protected int ChannelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmdContinue.Visible = false;
            if(!IsPostBack)
                grvManager_DataBind(); 
        }
        private void grvManager_DataBind()
        {
            ChannelID =  DatabaseHelper.SelectScalarIntFromID("ChannelID", "Panel", PanelID);
            DataTable la = DatabaseHelper.Select(new string[] { "Top " + field1 + " *" }, "Article", "ArticleID IN (Select ItemID From PanelList WHERE PanelID=" + PanelID + ") AND [Status]>2 AND PublishedTime<'" + cmdContinue.CommandArgument + "'", "PublishedTime DESC");
            this.grvLargeArticle.DataSource = la;
            this.grvLargeArticle.DataKeyNames = new string[] { "ArticleID" };
            this.grvLargeArticle.DataBind();
            if (la.Rows.Count.ToString() == field1)
            {
                string lastArticlePublishedTime = field1 == "0" ? cmdContinue.CommandArgument : DateTime.Parse(la.Rows[la.Rows.Count - 1]["PublishedTime"].ToString()).ToString("MM/dd/yyyy HH:mm:ss");
                DataTable sa = DatabaseHelper.Select(new string[] { "Top " + field2 + " *" }, "Article", "ArticleID IN (Select ItemID From PanelList WHERE PanelID=" + PanelID + ") AND [Status]>2 AND PublishedTime<'" + lastArticlePublishedTime + "'", "PublishedTime DESC");
                grvSmallArticle.DataSource = sa;
                grvSmallArticle.DataBind();
                if (sa.Rows.Count.ToString() == field2)
                {
                    lastArticlePublishedTime = field2 == "0" ? lastArticlePublishedTime : DateTime.Parse(sa.Rows[sa.Rows.Count - 1]["PublishedTime"].ToString()).ToString("MM/dd/yyyy HH:mm:ss");
                    object p = DatabaseHelper.SelectScalar("SELECT Top 1 PublishedTime FROM Article WHERE PanelID=" + PanelID + " AND [Status]>2 AND PublishedTime<'" + lastArticlePublishedTime + "' ORDER BY PublishedTime DESC");
                    if (p != null)
                    {
                        cmdContinue.Text = StringHelper.CaptionLanguage("Xem tiếp", "Continues");
                        cmdContinue.Visible = true;
                        cmdContinue.CommandArgument = DateTime.Parse(p.ToString()).ToString("MM/dd/yyyy HH:mm:ss");
                    }
                }
            }
        }
        public string WriteArticleImage(object ImageType,  int ArticleID_, string Url)
        {

            if (ImageType != System.DBNull.Value)
            {
                string st = "<a href='default.aspx?url=" + Url + "&ArticleID=" + ArticleID_ + "&PanelID=" + base.PanelID + "&ChannelID=" + ChannelID + "'>";
                st += "<img src='/imgView.aspx?ArticleID=" + ArticleID_ + "' class='HeadImage' />";
                st += "</a>";
                return st;
            }
            return string.Empty;
        }
        protected void cmdContinue_Click(object sender, EventArgs e)
        {
            grvManager_DataBind();
        }
    }
}